﻿(function($) {
    Sw.Namespace("$.sw");
    $.fn.extend({
        shadow: function(options) {
            return new $.sw.shadow(this, options);
        }
    });
    $.sw.shadow = Class({
        container: null,
        shadow: null,
        template: $.browser.msie ? '<div class="sw-ie-shadow"></div>' : '<div class="sw-shadow"><div class="sh"><div class="shl"></div><div class="shr"></div><div class="shc"></div></div><div class="sc"><div class="scl"></div><div class="scr"></div><div class="scc"></div></div><div class="sf"><div class="sfl"></div><div class="sfr"></div><div class="sfc"></div></div></div>',
        isRendered: $(".sw-ie-shadow").length > 0 || $(".sw-shadow").length > 0,
        initialize: function(container, options) {
            this.container = $(container);
            if (!this.isRendered) { $(document.body).append(this.template); }
            this.initComponent();
        },
        setPosition: function(element, val) {
            var size = Sw.Size.fromElement(this.container, 2).add(val.width, val.height);
            $(element).css({
                top: parseInt($(this.container)[0].style.top) - val.top,
                left: parseInt($(this.container)[0].style.left) - val.left,
                height: size.height,
                width: size.width,
                zIndex: parseInt(this.container.css("zIndex")) - val.zIndex
            });
        },
        show: function() { this.shadow.show(); },
        hide: function() { this.shadow.hide(); },
        initComponent: function() {
            var me = this;
            if ($.browser.msie) {
                this.shadow = $("div.sw-ie-shadow").css("filter", "progid:DXImageTransform.Microsoft.alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=4)").hide();
                this.setPosition(this.shadow, { top: 2, left: 4, height: -1, width: -1, zIndex: 1 });
            }
            else {
                this.shadow = $("div.sw-shadow").hide();
                this.setPosition(this.shadow, { top: 2, left: 3, height:6, width:6, zIndex: 1 });
                $(".sc", this.shadow).children().andSelf().each(function() {
                    $(this).css("height", me.container.outerHeight() - 4);
                });
            };
        },
        refresh: function() {

            this.initComponent();
            this.show();
        }
    });
})(jQuery);
